package com.happy.mycar.ui.Set;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.happy.mycar.CBLEDevice;
import com.happy.mycar.R;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.happy.mycar.databinding.FragmentSetBinding;

public class SetFragment extends Fragment {

    private FragmentSetBinding binding;

    private int nSetLockRSSI = 0;
    private int nSetUnLockRSSI = 0;

    private short nSetLockKey = 0;
    private short nSetUnLockKey = 0;
    private short nSetTrunkKey = 0;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentSetBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        var esp_config = CBLEDevice.getInstance().getEspConfig();
        nSetLockRSSI = esp_config.ESP32_RSSI_NEG_THRESHOLD;
        nSetUnLockRSSI = esp_config.ESP32_RSSI_POS_THRESHOLD;
        nSetLockKey = esp_config.LOCK_KEY;
        nSetUnLockKey = esp_config.UNLOCK_KEY;
        nSetTrunkKey = esp_config.TRUNK_KEY;

        Switch sw_autoLock = root.findViewById(R.id.switch_autolock);
        sw_autoLock.setSaveEnabled(false);
        sw_autoLock.setChecked(esp_config.AUTO_LOCK);
        sw_autoLock.setOnClickListener(v -> {
            esp_config.AUTO_LOCK = sw_autoLock.isChecked();
            esp_config.IS_Need_SAVE = true;
        });

        Switch sw_autoUnlock = root.findViewById(R.id.switch_autounlock);
        sw_autoUnlock.setSaveEnabled(false);
        sw_autoUnlock.setChecked(esp_config.AUTO_UNLOCK);

        sw_autoUnlock.setOnClickListener(v -> {
            esp_config.AUTO_UNLOCK = sw_autoUnlock.isChecked();
            esp_config.IS_Need_SAVE = true;
        });
        EditText textUnlock = root.findViewById(R.id.editTextUnLockRSSI);
        textUnlock.setSaveEnabled(false);
        textUnlock.setText(String.valueOf(esp_config.ESP32_RSSI_POS_THRESHOLD));
        textUnlock.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    int unlockRssi = Integer.parseInt(s.toString());
                    if (unlockRssi > -100 && unlockRssi < 0)
                        nSetUnLockRSSI = unlockRssi;
                } catch (NumberFormatException e) {
                }
            }
        });

        EditText textlock = root.findViewById(R.id.editTextLockRSSI);
        textlock.setSaveEnabled(false);
        textlock.setText(String.valueOf(esp_config.ESP32_RSSI_NEG_THRESHOLD));
        textlock.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    int lockRssi = Integer.parseInt(s.toString());
                    if (lockRssi > -100 && lockRssi < 0)
                        nSetLockRSSI = lockRssi;
                } catch (NumberFormatException e) {
                }
            }
        });

        EditText textLockKey = root.findViewById(R.id.editTextLockKey);
        textLockKey.setSaveEnabled(false);
        textLockKey.setText(String.valueOf(esp_config.LOCK_KEY));
        textLockKey.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    nSetLockKey = Short.parseShort(s.toString());
                } catch (NumberFormatException e) {
                }
            }
        });
        EditText textUnlockKey = root.findViewById(R.id.editTextUnLockKey);
        textUnlockKey.setSaveEnabled(false);
        textUnlockKey.setText(String.valueOf(esp_config.UNLOCK_KEY));
        textUnlockKey.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    nSetUnLockKey = Short.parseShort(s.toString());
                } catch (NumberFormatException e) {
                }
            }
        });
        EditText textTrunkKey = root.findViewById(R.id.editTextTrunkKey);
        textTrunkKey.setSaveEnabled(false);
        textTrunkKey.setText(String.valueOf(esp_config.TRUNK_KEY));
        textTrunkKey.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    nSetTrunkKey = Short.parseShort(s.toString());
                } catch (NumberFormatException e) {
                }
            }
        });

        return root;
    }

    @Override
    public void onDestroyView() {

        var esp_config = CBLEDevice.getInstance().getEspConfig();
        if (nSetLockRSSI != esp_config.ESP32_RSSI_NEG_THRESHOLD) {
            esp_config.ESP32_RSSI_NEG_THRESHOLD = nSetLockRSSI;
            esp_config.IS_Need_SAVE = true;
        }
        if (nSetUnLockRSSI != esp_config.ESP32_RSSI_POS_THRESHOLD) {
            esp_config.ESP32_RSSI_POS_THRESHOLD = nSetUnLockRSSI;
            esp_config.IS_Need_SAVE = true;
        }
        if (nSetLockKey != esp_config.LOCK_KEY) {
            esp_config.LOCK_KEY = nSetLockKey;
            esp_config.IS_Need_SAVE = true;
        }
        if (nSetUnLockKey != esp_config.UNLOCK_KEY) {
            esp_config.UNLOCK_KEY = nSetUnLockKey;
            esp_config.IS_Need_SAVE = true;
        }
        if (nSetTrunkKey != esp_config.TRUNK_KEY) {
            esp_config.TRUNK_KEY = nSetTrunkKey;
            esp_config.IS_Need_SAVE = true;
        }

        if (esp_config.IS_Need_SAVE) {
            esp_config.SaveConfig();
            Toast.makeText(getActivity(), "配置已保存", Toast.LENGTH_SHORT).show();
        }
        binding = null;
        super.onDestroyView();
    }
}